<?php namespace app\admin\controller;

use houdunwang\request\Request;
use system\model\Article as ArticleModel;
use system\model\Category;
use module\Wechat;
use system\model\Keywords;

class Article extends Common
{
    use Wechat;

    //动作
    public function index(ArticleModel $article)
    {
        //此处书写代码...
        $field = $article->paginate(v('config.article_num'));
        return view('', compact('field'));
    }

    //添加+编辑
    public function post(ArticleModel $article, Category $category)
    {
        $arc_id = Request::get('arc_id', 0, 'intval');
        //↑请求get参数获取当前文章id
        if (IS_POST) {
            if ($arc_id) {
                $article = ArticleModel::find($arc_id);
                //↑如果get中有id 寻找表内的该id
            }
            $post = Request::post();
            //↑获取post提交数据
            $article->save($post);
            //↑将数据存储到文章表

            //添加关键词表数据
            $data = ['keywords' => $post['arc_keyword'], 'module_id' => $arc_id, 'module' => 'article'];
            //↑通过post中的关键词 生成关键表中关键词、对应模块、模块id
            $this->saveKeywords($data);
            //↑将数据存入关键词表

            $this->setRedirect('index')->success('操作成功');
            //↑操作提示返回列表
        }
        if ($arc_id) {
            //编辑
            $oldData = ArticleModel::find($arc_id);
//            p($oldData->toArray());
        }
        //获取所属栏目数据
        $cateData = $category->getAllCate();
        return view('', compact('cateData', 'oldData'));
    }

    //删除
    public function remove(ArticleModel $article)
    {
        $arc_id = Request::get('arc_id');
        //执行删除
        $model = ArticleModel::find($arc_id);
//        p($model);die;
        $keyword = Keywords::where('keywords',$model['arc_keyword'])->first();
//        p($keyword);用文章表中关键词获取关键词表id
        $keyword->destory();
//       删除关键词
        $model->destory();
//        删除文章
        return $this->setRedirect('index')->success('操作成功');
        //操作提示返回列表

//		go('index');
    }
}
